<?php
function reequipment_adminapi_updateitems($args)
{
    // Get arguments from argument array
    extract($args);

    /* Fields in table.
    item_id
	part_number
	model_id
	equipment_id                                                                                                       
	company_id                                                                                                           
	last_ordered                   
	man_part_number
	cost
   */

    // Get datbase setup
    $dbconn =& xarDBGetConn();
    $xartable =& xarDBGetTables();
    $table = $xartable['reitems'];
    $update = array();
    $bindvars = array();

    // Update item
    $query = "UPDATE $table SET ";
    if (isset($item_id)){
        $update[] = "item_id = ? ";
        $bindvars[] = $item_id;
    }
    if (isset($part_number)){
        $update[] = "part_number = ? ";
        $bindvars[] = $part_number;
    }
    if (isset($invoice_number)){
        $update[] = "invoice_number = ? ";
        $bindvars[] = $invoice_number;
    }
    if (isset($description)){
        $update[] = "description = ? ";
        $bindvars[] = $description;
    }
    if (isset($model_id)){
        $update[] = "model_id = ? ";
        $bindvars[] = $model_id;
    }
    if (isset($equipment_id)){
        $update[] = "equipment_id = ? ";
        $bindvars[] = $equipment_id;
    }
    if (isset($company_id)){
        $update[] = "company_id = ? ";
        $bindvars[] = $company_id;
    }
    if (isset($last_ordered)){
        $update[] = "last_ordered = ? ";
        $bindvars[] = $last_ordered;
    }
    if (isset($man_part_number)){
        $update[] = "man_part_number = ? ";
        $bindvars[] = $man_part_number;
    } 
    if (isset($cost)){
        $update[] = "cost = ? ";
        $bindvars[] = $cost;
    }        
    $query .= join(",",$update);
    $query .= "WHERE item_id = ? ";
    $bindvars[] = $item_id;
    $result =& $dbconn->Execute($query, $bindvars);
    if (!$result) return;
    // Let any hooks know that we have created a new topic
    $args['module'] = 'reequipment';
    $args['itemtype'] = 5; //tag
    $args['itemid'] = $item_id;
    xarModCallHooks('item', 'update', $item_id, $args);
    
    // Need to delete the cycle relationship and recreate it.
    $cycles = xarModAPIFunc('reequipment', 'admin', 'deleterelcycles', array('item_id' => $item_id));
    $cycles = xarModAPIFunc('reequipment', 'admin', 'createrelcycles', array('equipment_id' => $equipment_id, 'item_id' => $item_id));
    
    // Return the id of the newly created link to the calling process
    return true;
}
?>